package com.huawei.admins.platform.gateway.core.component.filter;

import com.huawei.admins.platform.gateway.core.util.NameUtils;

import java.util.Map;

public interface GatewayFilterFactory {

    GatewayFilter apply(String routeId, FilterDefinition definition);

    default String name() {
        return NameUtils.normalizeFilterFactoryName(getClass());
    }

    @SuppressWarnings("unchecked")
    default <T> T getOrDefault(Map<String, String> configValue, String key, T defaultValue) {
        if (configValue == null) {
            return defaultValue;
        }
        String value = configValue.get(key);
        if (value == null) {
            return defaultValue;
        }
        Class<?> valueType = defaultValue.getClass();
        if (String.class.isAssignableFrom(valueType)) {
            return (T) value;
        }
        if (Boolean.class.isAssignableFrom(valueType)) {
            return (T) Boolean.valueOf(value);
        }
        if (Long.class.isAssignableFrom(valueType)) {
            return (T) Long.valueOf(value);
        }

        return defaultValue;
    }
}
